# -*- coding: UTF-8 -*-
from argparse import ArgumentParser
from txydomain import *
from txydomain.api import Login
from txydomain import current
from sys import exit
from pathlib import Path
from Read.ini import ReadIni


def main(ipaddress_, file_):
    """
    域名解析主函数
    :param ipaddress_: 设置标准IP地址
    :param file_: 配置文件
    :return:
    """
    global debug
    config_file = Path(file_)
    if not config_file.is_file():
        exit("找不到配置文件: %s" % file_)
    read_ini = ReadIni(file_)

    ip = ""
    domain_ = read_ini.get_value(sec="domain", key="name")
    sub_domain = read_ini.get_value(sec="domain", key="sub")

    token_ = read_ini.get_value(sec="login", key="token")
    key_ = read_ini.get_value(sec="login", key="key")
    id_ = read_ini.get_value(sec="login", key="id")
    token_ = str(token_).replace(" ", "").replace("'", "").replace('"', '').lstrip()
    key_ = str(key_).replace(" ", "").replace("'", "").replace('"', '').lstrip()
    id_ = str(id_).replace(" ", "").replace("'", "").replace('"', '').lstrip()
    if debug:
        print("key: -> ", key_)
        print("token: -> ", token_)
        print("id_: -> ", id_)
        print("域名: -> ", domain_)
        print("子域名: -> ", sub_domain)
    print("当前IP: -> ", ipaddress_)
    RecordId = ""
    RecordLine = ""
    print("正在检测域名")
    # up = Start(secret_id=id_,
    #            secret_key=key_,
    #            token=token_,
    #            domain=domain_)
    # info = up.info()
    # print(info)
    get = Login(secret_id=id_,
                secret_key=key_,
                token=token_,
                mode="api",
                domain_=domain_,
                sub_domain=sub_domain)

    domain_list = get.get_record()["RecordList"]
    # print(domain_list)
    ip_list = []
    for i in domain_list:
        ip = i["Value"]
        sub_domain = i["Name"]
        RecordId = i["RecordId"]
        RecordLine = i["Line"]
        ip_list.append(ip)
        print("解析记录: %s.%s -> %s" % (sub_domain, domain_, ip))
    if str(ipaddress_) not in ip_list:
        print("检测到记录已过期")
        print("当前记录IP: ", ip)
        print("当前实际IP: ", ipaddress_)
        print("正在更新")
        status = get.update(ip=ipaddress_,
                            record_id=RecordId,
                            record_line=RecordLine)
        if status:
            print("更新成功")
        else:
            print("更新失败")
    else:
        print("当前设置与本地IP一致，无需更新")


if __name__ == "__main__":
    py_ver = "1.0.12101"
    arg = ArgumentParser(description='当前脚本版本: %s' % py_ver, prog="腾讯云DDNS自动解析")
    arg.add_argument('-c', '--config', type=str, default="./config/login.conf",
                     help='设置腾讯云登录配置文件, 默认: ./config/login.conf', required=False)
    arg.add_argument('-a', '--address', type=str,
                     help="设置当前公网IP地址, 默认自动检查", required=False)
    arg.add_argument('-d', '--debug', type=int, default=0,
                     help="设置显示详细信息，默认不显示(1: 显示， 0: 不显示)", required=False)
    args = arg.parse_args()
    address = args.address
    file = args.config
    d = args.debug
    debug = False
    if int(d) == 1:
        debug = True
    if not address:
        address = current.Get_address()
    if address:
        main(file_=file, ipaddress_=address)
    else:
        exit("未设置当前公网IP且自动获取失败")
